var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var type_id= require("./type");
var ArticleSchema = new Schema({
	id: {
		type: Number,
		default: 0
	},
	title: String,
	alias: String,
	keywords:String,
	remark: String,
	categoryId: {
		type: ObjectId,
		ref: 'category'
	},
    typeId: {
        type: Number,
    },
	thumbnail:String,
	content:String,
	isIndex:{
		type: Boolean,
		default:false
	},
    clicks: {
        type: Number,
        default:0
    },
	author: {
		type: ObjectId,
		ref: 'Admin'
	},
	publish: {
		type: Date,
		default: Date.now
	}
});

//使用findByIdAndUpdate进行ID自增，将返回值重新插入留言表里
//主建id自增
ArticleSchema.pre('save', function(next) {
    var doc = this;
    type_id.findByIdAndUpdate({_id:'ids'},{$inc: { 'ids.aid': 1} }, function(error, counter)   {
        if(error)
            return next(error);
        doc.id = counter.ids.aid;
        next();
    });
});
var Article = mongoose.model('article', ArticleSchema, 'article');
module.exports = Article;